package com.gxa.p2p.controller;
import com.gxa.p2p.common.JsonResult;
import com.gxa.p2p.pojo.Article;
import com.gxa.p2p.pojo.Management;
import com.gxa.p2p.service.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 邓邦武
 * @author HS
 */
@CrossOrigin
@RestController
@RequestMapping("/article")

@Api(value = "文章操作接口")

public class ArticleController {

    @Autowired
    private ArticleService service=null;


    /**
     * 邓邦武
     */
    @PostMapping("/findArticleById")
    @ApiOperation(value = "文章的编号查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "编号",required = true)
    })
    public Map findArticleById(@RequestParam Long id){

        Article article = service.findArticleById(id);

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        jsonResult.setData(article);
        return jsonResult.getDataMap();
    }
    /**
     * 邓邦武
     */
    @PostMapping("/findArticleByPage")
    @ApiOperation(value = "文章的分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name="title",value = "标题",required = false),
            @ApiImplicitParam(name="type",value = "分类",required = false),
            @ApiImplicitParam(name="state",value = "状态",required = false),
            @ApiImplicitParam(name="page",value = "页码",required = true),
            @ApiImplicitParam(name="size",value = "每页记录数",required = true)

    })
    public Map findArticleByPage(@RequestBody Map map){

        Map<Integer, List<Article>> resultMap = service.findArticleByPage(map);
        Integer count=0;
        Set<Integer> set = resultMap.keySet();
        for (Integer integer : set) {
            count=integer;
        }
        List<Article> list = resultMap.get(count);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        jsonResult.setData(list);
        jsonResult.getDataMap().put("count",count);
        return jsonResult.getDataMap();
    }

    /**
     * 邓邦武
     * @param article
     * @return
     */
    @PostMapping("/addArticle")
    @ApiOperation(value = "增加文章")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "article",value = "文章信息",required = true,paramType = "body")
    })
    public Map addArticle(@RequestBody Article article){

        service.addArticle(article);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");

        return jsonResult.getDataMap();
    }

    /**
     * 邓邦武
     * @param id
     * @return
     */
    @PostMapping("/deleteArticle")
    @ApiOperation(value = "删除文章")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "文章编号",required = true)
    })
    public Map deleteArticle(@RequestParam("id") Long id){

        service.deleteArticle(id);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        return jsonResult.getDataMap();
    }

    /**
     * 邓邦武
     * @param article
     * @return
     */
    @PostMapping("/updateArticle")
    @ApiOperation(value = "修改文章")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Article",value = "文章信息",required = true,paramType = "Article")
    })
    public Map updateAgreement(@RequestBody Article article){

        service.updateArticle(article);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        return jsonResult.getDataMap();
    }

}
